What is uid?
The uid npm package is a simple utility for generating unique IDs of variable length. It is commonly used when a short, unique identifier is needed, such as for database keys, component identifiers in a UI, or any other case where a string ID is required.
What are uid's main functionalities?
Generating a UID of default length (11 characters)
This feature generates a unique identifier string with a default length of 11 characters. The generated ID is a random string that can be used where a unique identifier is needed.
"use strict";
const uid = require('uid');
const id = uid();
console.log(id);
Generating a UID of a specific length
This feature allows the generation of a unique identifier string of a specific length. The length is passed as an argument to the uid function, allowing for IDs of varying sizes to be created according to the needs of the application.
"use strict";
const uid = require('uid');
const length = 16;
const id = uid(length);
console.log(id);
Other packages similar to uid
uuid
The uuid package is a more feature-rich library for generating UUIDs (Universally Unique Identifiers) according to various standards like RFC4122. It supports version 1 (timestamp-based), version 3 (namespace-based with MD5), version 4 (random), and version 5 (namespace-based with SHA1). Compared to uid, uuid offers more options and follows standardized formats, which can be important for interoperability and meeting certain technical specifications.
shortid
The shortid package is designed to create short, non-sequential, URL-friendly unique IDs. It is more customizable than uid, allowing for the definition of an alphabet to use for ID generation and the ability to avoid using similar-looking characters to reduce user error when typing. However, the shortid package is no longer recommended for use as it has been deprecated in favor of the nanoid package.
nanoid
Nanoid is a tiny, secure, URL-friendly, unique string ID generator for JavaScript. It is similar to uid in its simplicity and ease of use but offers a higher level of customization and security. Nanoid allows for custom alphabets and sizes, and it uses a more secure random number generator. It is a modern alternative to both uid and shortid, suitable for applications where unique identifiers are critical.
uid
A tiny (134B) and fast utility to randomize unique IDs of fixed length
Fast object IDs. Available for Node.js and the browser.
Generate randomized output strings of fixed length using lowercase alphanumeric characters (a-z0-9
).
Notice: Please note that this is not a cryptographically secure (CSPRNG) generator.
Additionally, this module is delivered as:
Install
$ npm install --save uid
Usage
import uid from 'uid';
uid();
uid();
uid(16);
uid(25);
uid(32);
API
uid(length?)
Returns: String
Creates a new random identifer of fixed length.
length
Type: Number
Default: 11
Then length of the output string.
Important: Your risk of collisions decreases with longer strings!
Benchmarks
Running on Node.js v10.13.0
Validation (length = 11):
✔ hashids/fixed (example: "QWjnegYbwZ1")
✔ nanoid/non-secure (example: "o9SPLfEtDMB")
✔ nanoid (example: "BbgkcjUV8fg")
✔ uid (example: "5j2t6tmjboz")
Benchmark (length = 11):
hashids/fixed x 358,452 ops/sec ±2.04% (95 runs sampled)
nanoid/non-secure x 4,855,107 ops/sec ±0.33% (96 runs sampled)
nanoid x 493,479 ops/sec ±0.42% (96 runs sampled)
uid x 5,034,240 ops/sec ±0.24% (94 runs sampled)
Validation (length = 25):
✔ cuid (example: "ck7dod7qj0000ws7c5cmmh5mc")
✔ hashids/fixed (example: "r9JOyLkQWjnegYbwZ1p0GDXNm")
✔ nanoid/non-secure (example: "aIrSCUwGMsMSZ-1xnSB8myg0X")
✔ nanoid (example: "51jS9SkdKG5lXW5Yg3L4juzuT")
✔ uid (example: "k0gd21k1p2y7qgwmgrgspo4uy")
Benchmark (length = 25):
cuid x 160,075 ops/sec ±1.35% (90 runs sampled)
hashids/fixed x 337,598 ops/sec ±0.13% (98 runs sampled)
nanoid/non-secure x 2,246,032 ops/sec ±0.31% (96 runs sampled)
nanoid x 431,758 ops/sec ±0.71% (98 runs sampled)
uid x 2,417,171 ops/sec ±0.33% (96 runs sampled)
Validation (length = 36):
✔ uuid/v1 (example: "e3304870-5e48-11ea-93a8-0d27db144950")
✔ uuid/v4 (example: "8f18bfb3-45f9-4c14-b949-87ed98cc41c1")
✔ hashids/fixed (example: "EVq3Pr9JOyLkQWjnegYbwZ1p0GDXNmRBlAxg")
✔ @lukeed/uuid (example: "c47bd63e-7975-47a6-96c2-ea6744c4e0c2")
✔ nanoid/non-secure (example: "4yBHuV2LX1z6uYF2htLIq1dBK-Bqt4r-Il-Q")
✔ nanoid (example: "I01YCalTULD9SorD6lWzDp30hL1_JbULU8UR")
✔ uid (example: "599tis8bq39vxc95vkfxxr80gwi1mnhuiorz")
Benchmark (length = 36):
uuid/v1 x 1,485,783 ops/sec ±0.27% (97 runs sampled)
uuid/v4 x 331,019 ops/sec ±0.86% (91 runs sampled)
hashids/fixed x 314,980 ops/sec ±2.07% (96 runs sampled)
@lukeed/uuid x 6,355,015 ops/sec ±0.43% (94 runs sampled)
nanoid/non-secure x 1,658,583 ops/sec ±0.23% (95 runs sampled)
nanoid x 402,224 ops/sec ±0.15% (97 runs sampled)
uid x 1,719,753 ops/sec ±0.24% (95 runs sampled)
Related
- @lukeed/uuid - A tiny (230B), fast, and cryptographically secure UUID (V4) generator for Node and the browser
Credits
Thank you Matthew Mueller for gifting the uid
name on npm.
This module was previously known as foid
(fast object IDs).
License
MIT © Luke Edwards